package com.corpgovernment.organization.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.corpgovernment.api.organization.model.user.NameInfoBo;
import com.corpgovernment.common.apollo.OrganizationApollo;
import com.corpgovernment.common.constant.RegularExpressionConst;
import com.corpgovernment.common.enums.CardEnum;
import com.corpgovernment.common.enums.GenderEnum;
import com.corpgovernment.common.enums.NationalityEnum;
import com.corpgovernment.common.enums.PhoneCountryCodeEnum;
import com.corpgovernment.common.mq.enums.MessageBizTypeEnum;
import com.corpgovernment.common.tuple.Tuple2;
import com.corpgovernment.common.utils.DateUtil;
import com.corpgovernment.common.utils.HttpUtils;
import com.corpgovernment.common.utils.Md5Util;
import com.corpgovernment.organization.entity.SyncEmployeeCallBackRequestEntity;
import com.corpgovernment.organization.entity.SyncEmployeeRequestEntity;
import com.corpgovernment.organization.entity.db.*;
import com.corpgovernment.organization.enumm.ImportExcelRecordDetailLogErrorMessageEnum;
import com.corpgovernment.organization.enumm.MbImportExcelRecordStatusEnum;
import com.corpgovernment.organization.enumm.MbImportExcelRecordTypeEnum;
import com.corpgovernment.organization.enumm.ServiceErrorEnum;
import com.corpgovernment.organization.model.OpenCardEmployeeModel;
import com.corpgovernment.organization.model.SupplierCompanyModel;
import com.corpgovernment.organization.service.*;
import com.corpgovernment.organization.util.IdCardUtil;
import com.corpgovernment.organization.util.StrUtils;
import com.corpgovernment.redis.handler.RedisHandler;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import com.ctrip.corp.obt.generic.utils.JsonUtils;
import com.ctrip.corp.obt.generic.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author ：llw
 * @date ：Created in 2020/12/11 19:57
 * @description：
 * @modified By：
 * @version: $
 */
@Service
@Slf4j
public class SyncEmployeeServiceImpl implements ISyncEmployeeService {

    @Autowired
    private IOrganizationEmployeeService organizationEmployeeService;
    @Autowired
    private IOrganizationInfoService organizationInfoService;
    @Autowired
    private IPostService postService;
    @Autowired
    private IMbImportExcelRecordService mbImportExcelRecordService;
    @Autowired
    private RedisHandler redisHandler;
    @Autowired
    private OrganizationApollo organizationApollo;
    @Autowired
    private IOrgUserIdCardService organizationUserIdCardService;
    @Autowired
    private CtripOpenCardService ctripOpenCardService;

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @Override
    public Tuple2<ServiceErrorEnum, Long> addEmployee(SyncEmployeeRequestEntity.SyncEmployeeInfoEntity employee) {

        // 员工参数校验
        ServiceErrorEnum checkResult = checkEmployeeRequest(employee, true);
        if (checkResult != ServiceErrorEnum.Success) {
            return new Tuple2<>(checkResult, null);
        }

        MbOrgEmployeeInfo emp = new MbOrgEmployeeInfo();
        List<MbOrgUserIdcard> cardTypes = new ArrayList<>();
        List<MbUserOrgRelation> orgRelation = new ArrayList<>();
        String errorMessage = setParam(employee, emp, cardTypes, orgRelation);
        if(!StringUtils.isBlank(errorMessage)){
            ServiceErrorEnum serviceErrorEnum = ServiceErrorEnum.ChangeAble_Error;
            serviceErrorEnum.setErrorMessage(errorMessage);
            return new Tuple2<>(serviceErrorEnum, null);
        }
        ServiceErrorEnum addErrorEnum = organizationEmployeeService.addEmployee(emp, cardTypes, null, orgRelation);
        if (addErrorEnum != ServiceErrorEnum.Success) {
            return new Tuple2<>(addErrorEnum, null);
        }
        return new Tuple2<>(ServiceErrorEnum.Success, 0L);
    }


    /**
     * 修改员工
     *
     * @param employee
     * @return
     */
    @Override
    public Tuple2<ServiceErrorEnum, Long> updateEmployee(SyncEmployeeRequestEntity.SyncEmployeeInfoEntity employee) {

        // 员工参数校验
        ServiceErrorEnum checkResult = checkEmployeeRequest(employee, false);
        if (checkResult != ServiceErrorEnum.Success) {
            return new Tuple2<>(checkResult, null);
        }

        MbOrgEmployeeInfo emp = new MbOrgEmployeeInfo();
        List<MbOrgUserIdcard> cardTypes = new ArrayList<>();
        List<MbUserOrgRelation> orgRelation = new ArrayList<>();
        String errorMessage =  setParam(employee, emp, cardTypes, orgRelation);
        if(!StringUtils.isBlank(errorMessage)){
            ServiceErrorEnum serviceErrorEnum = ServiceErrorEnum.ChangeAble_Error;
            serviceErrorEnum.setErrorMessage(errorMessage);
            return new Tuple2<>(serviceErrorEnum, null);
        }
        ServiceErrorEnum updateErrorEnum = organizationEmployeeService.updateEmployee(emp, cardTypes, null, orgRelation);
        if (updateErrorEnum != ServiceErrorEnum.Success) {
            return new Tuple2<>(updateErrorEnum, null);
        }
        return new Tuple2<>(ServiceErrorEnum.Success, 0L);
    }

    /**
     * 参数赋值
     *
     * @param employee
     * @param emp
     * @param cardTypes
     * @param orgRelation
     */
    private String setParam(SyncEmployeeRequestEntity.SyncEmployeeInfoEntity employee, MbOrgEmployeeInfo emp, List<MbOrgUserIdcard> cardTypes, List<MbUserOrgRelation> orgRelation) {
        StringBuilder errorMessage = new StringBuilder();
        if (employee == null) {
            return null;
        }

        if (emp != null) {
            emp.setUid(employee.getBasicInfo().getUid());
            emp.setName(employee.getBasicInfo().getName());
            emp.setValid("T");
            emp.setMobilePhone(employee.getBasicInfo().getMobilePhone());
            emp.setAreaCode(employee.getBasicInfo().getCountryCode());
            emp.setEmail(employee.getBasicInfo().getEmail());
            emp.setGender(employee.getBasicInfo().getGender());
            emp.setBirthday(StringUtils.isEmpty(employee.getBasicInfo().getBirthday()) ? null : DateUtil.stringToDate(employee.getBasicInfo().getBirthday(), DateUtil.DF_YMD));
            emp.setFirstEnName(employee.getBasicInfo().getFirstName());
            emp.setLastEnName(employee.getBasicInfo().getLastName());
            emp.setNationality(employee.getBasicInfo().getNationality());
            emp.setIsBackground(false);
            emp.setPwd(employee.getBasicInfo().getPassword());
            emp.setCreditCardNumbers(employee.getBasicInfo().getCreditCardNumbers());
            NameInfoBo nameInfoBo = new NameInfoBo();
            nameInfoBo.setSurname(employee.getBasicInfo().getLastName());
            nameInfoBo.setGivenname(employee.getBasicInfo().getFirstName());
            nameInfoBo.setFullName(employee.getBasicInfo().getName());
            nameInfoBo.setNationality(employee.getBasicInfo().getNationality());
            if(StrUtils.checkNameInfo(nameInfoBo)){
                emp.setNameInfo(JsonUtils.toJsonString(nameInfoBo));
            }
        }

        if (cardTypes != null && CollectionUtils.isNotEmpty(employee.getCardTypeList())) {
            cardTypes.addAll(employee.getCardTypeList().stream().map(ct -> {
                MbOrgUserIdcard temp = new MbOrgUserIdcard();
                temp.setCardType(ct.getCardType());
                temp.setCardNo(ct.getCardNo());
                temp.setTimeLimit(StringUtils.isEmpty(ct.getTimeLimit()) ? null : DateUtil.stringToDate(ct.getTimeLimit(), DateUtil.DF_YMD));
                temp.setEmployeeUid(employee.getBasicInfo().getUid());
                return temp;
            }).collect(Collectors.toList()));
        }

        if (orgRelation != null && CollectionUtils.isNotEmpty(employee.getOrgRelation())) {
            orgRelation.addAll(employee.getOrgRelation().stream().map(or -> {
                MbUserOrgRelation temp = new MbUserOrgRelation();
                if (StringUtils.isEmpty(or.getOrgId())) {
                    errorMessage.append(MessageFormat.format("{0}的组织id为空", employee.getBasicInfo().getUid()));
                    return temp;
                }
                MbOrgInfo companyInfo = organizationInfoService.findRecentCompanyIdByOrgId(or.getOrgId());
                if (companyInfo != null) {
                    MbPostInfo postInfo = postService.findByNameAndOrgId(or.getPostName(), companyInfo.getOrgId());
                    temp.setPostId(Optional.ofNullable(postInfo).map(MbPostInfo::getId).orElse(null));
                } else {
                    errorMessage.append(MessageFormat.format("{0}的上级公司id不存在", employee.getBasicInfo().getUid()));
                    return temp;
                }
                if (!StringUtils.isEmpty(employee.getOperatorCorpId()) && !companyInfo.getOrgId().equals(employee.getOperatorCorpId())) {
                    errorMessage.append(MessageFormat.format("操作人无权限添加人员{0}，组织信息{1}，该组织id不在操作人公司下", employee.getBasicInfo().getUid(), or.getOrgId()));
                    return temp;
                }
                temp.setOrgId(or.getOrgId());
                temp.setUid(employee.getBasicInfo().getUid());
                temp.setStatus(1);
                return temp;
            }).filter(x -> !StringUtils.isEmpty(x.getOrgId())).collect(Collectors.toList()));
        }
        return errorMessage.toString();
    }

    /**
     * 删除员工
     *
     * @param employee
     * @return
     */
    @Override
    public Tuple2<ServiceErrorEnum, Long> deleteEmployee(SyncEmployeeRequestEntity.SyncEmployeeInfoEntity employee) {

        // 组织架构参数校验
        if (employee == null || employee.getBasicInfo() == null || StringUtils.isEmpty(employee.getBasicInfo().getUid())) {
            ServiceErrorEnum serviceErrorEnum = ServiceErrorEnum.SyncHumanResources_NewError;
            serviceErrorEnum.setErrorMessage(ImportExcelRecordDetailLogErrorMessageEnum.G.getErrorMessage());
            return new Tuple2<>(serviceErrorEnum, null);
        }
        StringBuilder errorMessage = new StringBuilder();
        if (CollectionUtils.isNotEmpty(employee.getOrgRelation())) {
            employee.getOrgRelation().forEach(or -> {
                if (StringUtils.isEmpty(or.getOrgId())) {
                    errorMessage.append(MessageFormat.format("{0}的组织id为空", employee.getBasicInfo().getUid()));
                    return;
                }
                MbOrgInfo companyInfo = organizationInfoService.findRecentCompanyIdByOrgId(or.getOrgId());
                if (companyInfo == null) {
                    errorMessage.append(MessageFormat.format("{0}的上级公司id不存在", employee.getBasicInfo().getUid()));
                    return;
                }
                if (!StringUtils.isEmpty(employee.getOperatorCorpId()) && (companyInfo == null || !companyInfo.getOrgId().equals(employee.getOperatorCorpId()))) {
                    errorMessage.append(MessageFormat.format("操作人无权限添加人员{0}，组织信息{1}，该组织id不在操作人公司下", employee.getBasicInfo().getUid(), or.getOrgId()));
                }
            });
        }
        if (errorMessage.length() > 0) {
            ServiceErrorEnum serviceErrorEnum = ServiceErrorEnum.ChangeAble_Error;
            serviceErrorEnum.setErrorMessage(errorMessage.toString());
            return new Tuple2<>(serviceErrorEnum,null);
        }
        // 删除组织，逻辑同页面点击删除
        MbOrgEmployeeInfo deleteRequest = new MbOrgEmployeeInfo();
        deleteRequest.setUid(employee.getBasicInfo().getUid());
        ServiceErrorEnum deleteErrorEnum = organizationEmployeeService.deleteEmployee(deleteRequest);
        if (deleteErrorEnum != ServiceErrorEnum.Success) {
            return new Tuple2<>(deleteErrorEnum, null);
        }
        return new Tuple2<>(ServiceErrorEnum.Success, 0L);
    }

    /**
     * 检测员工参数
     *
     * @param employee
     * @return
     */
    private ServiceErrorEnum checkEmployeeRequest(SyncEmployeeRequestEntity.SyncEmployeeInfoEntity employee, boolean isAdd) {
        // 空值判断
        if (employee == null) {
            log.error("checkEmployeeRequest employee 空值判断");
            return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ErrorRequest_Failure;
        }
        // 默认不删除
        employee.setIsDelete(Optional.ofNullable(employee.getIsDelete()).orElse(false));

        // 空值判断
        if (employee.getBasicInfo() == null
                || StringUtils.isEmpty(employee.getBasicInfo().getUid())
                || StringUtils.isEmpty(employee.getBasicInfo().getName())
                || StringUtils.isEmpty(employee.getBasicInfo().getCountryCode())
                || StringUtils.isEmpty(employee.getBasicInfo().getMobilePhone())) {
            log.error("checkEmployeeRequest employee.getBasicInfo() 空值判断");
            return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_EmptyBasicInfo_Failure;
        }
        if (employee.getBasicInfo() != null) {
            PhoneCountryCodeEnum phoneCountryCodeEnum = StringUtils.isEmpty(employee.getBasicInfo().getCountryCode()) ? null : PhoneCountryCodeEnum.getByCountryCode(employee.getBasicInfo().getCountryCode());
            if (phoneCountryCodeEnum == null) {
                // 默认中国大陆
                employee.getBasicInfo().setCountryCode(PhoneCountryCodeEnum.CN.getCountryCode());
            }
            if (!StringUtils.isEmpty(employee.getBasicInfo().getEmail()) && !employee.getBasicInfo().getEmail().matches(RegularExpressionConst.MAIL)) {
                return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ContactInfo_EmailFormat_Failure;
            }
            Pattern p = Pattern.compile(RegularExpressionConst.PHONE);
            if (!StringUtils.isEmpty(employee.getBasicInfo().getMobilePhone()) && !p.matcher(employee.getBasicInfo().getMobilePhone()).find()) {
                return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ContactInfo_PhoneFormat_Failure;
            }
        }
        // 密码默认手机号
        if (isAdd) {
            String mobilePhone = employee.getBasicInfo().getMobilePhone();
            employee.getBasicInfo().setPassword(Optional.ofNullable(employee.getBasicInfo().getPassword()).orElse(mobilePhone.substring(mobilePhone.length() - 8)));
        }
        // 生日超出日期范围
        if (StringUtils.isNotBlank(employee.getBasicInfo().getBirthday())) {
            Date birth = DateUtil.stringToDate(employee.getBasicInfo().getBirthday(), DateUtil.DF_YMD);
            if (birth == null) {
                log.error("checkEmployeeRequest employee.getBasicInfo().getBirthday()为{} 超出日期范围", employee.getBasicInfo().getBirthday());
                return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ErrorBirthday_Failure;
            }
        }
        // 性别超出赋值范围，不赋值则默认男性
        employee.getBasicInfo().setGender(Optional.ofNullable(employee.getBasicInfo().getGender()).orElse(GenderEnum.M.getCode()));
        if (!StrUtils.In(employee.getBasicInfo().getGender(), GenderEnum.F.getCode(), GenderEnum.M.getCode())) {
            log.error("checkEmployeeRequest employee.getBasicInfo().getGender()为{} 超出赋值范围F或M", employee.getBasicInfo().getGender());
            return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ErrorGender_Failure;
        }
        // 国籍超出赋值范围，不赋值则默认中国大陆
        NationalityEnum nation = NationalityEnum.getByCode(Optional.ofNullable(employee.getBasicInfo().getNationality()).orElse(NationalityEnum.CN.getCode()));
        if (nation == null) {
            log.error("checkEmployeeRequest employee.getBasicInfo().getNationality()为{} 超出赋值范围", employee.getBasicInfo().getNationality());
            return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ErrorNationality_Failure;
        }
        // 按前端格式赋值 CN|中国大陆
        employee.getBasicInfo().setNationality(String.join("|", nation.getCode(), nation.getDesc()));

        if (CollectionUtils.isNotEmpty(employee.getCardTypeList())) {
            for (SyncEmployeeRequestEntity.SyncEmployeeInfoEntity.CardTypeEntity cardType : employee.getCardTypeList()) {
                // 空值判断
                if (cardType == null || StringUtils.isEmpty(cardType.getCardNo()) || cardType.getCardType() == null) {
                    log.error("checkOrganizationRequest org.getCardTypeList() 空值判断");
                    return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_EmptyCardType_Failure;
                }
                // 证件类型超出赋值范围
                if (CardEnum.getByType(cardType.getCardType().toString()) == null) {
                    log.error("checkEmployeeRequest employee.getCardTypeList().getCardType()为{} 超出赋值范围", cardType.getCardType());
                    return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_OutOfCardType_Failure;
                }
                // 证件有效期超出日期范围
                if (StringUtils.isNotBlank(cardType.getTimeLimit())) {
                    Date birth = DateUtil.stringToDate(cardType.getTimeLimit(), DateUtil.DF_YMD);
                    if (birth == null) {
                        log.error("checkEmployeeRequest employee.getCardTypeList().getTimeLimit()为{} 超出日期范围", cardType.getTimeLimit());
                        return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ErrorCardTypeTimeLimit_Failure;
                    }
                }
                //增加身份证校验
                if (Objects.equals(CardEnum.ID_CARD.getType(), cardType.getCardType().toString())) {
                    //身份证正则
                    if (!cardType.getCardNo().matches(RegularExpressionConst.CARD)) {
                        return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ContactInfo_IdCard_Failure;
                    }
                    //身份证和生日
                    if (!IdCardUtil.validDate(cardType.getCardNo(), employee.getBasicInfo().getBirthday())) {
                        return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ContactInfo_IdCard_Birthday_Failure;
                    }
                    //身份证和性别校验
                    if (StringUtils.isBlank(employee.getBasicInfo().getGender()) || employee.getBasicInfo().getGender().equals("M")) {
                        if (!IdCardUtil.getSex(cardType.getCardNo()).equals("男")) {
                            return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ContactInfo_IdCard_Sex_Failure;
                        }
                    } else {
                        if (!IdCardUtil.getSex(cardType.getCardNo()).equals("女")) {
                            return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ContactInfo_IdCard_Sex_Failure;
                        }
                    }
                    //身份证唯一性
                }
                    //检查证件是否重复，重复判断依据是 type+number唯一
                List<MbOrgUserIdcard> cards = organizationUserIdCardService.findByTypeAndNo(Integer.valueOf(cardType.getCardType()), cardType.getCardNo());
                if (CollUtil.isNotEmpty(cards)) {
                    return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_ContactInfo_Duplicate_Documents_Failure;
                }
            }
            // 去除null项
            employee.getCardTypeList().removeIf(Objects::isNull);
        }

        // 空值判断
        if (isAdd && (CollectionUtils.isEmpty(employee.getOrgRelation())
                || employee.getOrgRelation().stream().anyMatch(or -> or != null && StringUtils.isEmpty(or.getOrgId())))) {
            log.error("checkEmployeeRequest employee.getOrgRelation() 空值判断");
            return ServiceErrorEnum.SyncHumanResources_SyncEmployeeInfo_EmptyRelationOrgId_Failure;
        }
        // 去除null项
        Optional.ofNullable(employee.getOrgRelation()).orElse(new ArrayList<>()).removeIf(Objects::isNull);

        return ServiceErrorEnum.Success;
    }

    @Override
    public void syncEmployeeInfo(SyncEmployeeRequestEntity employeeInfo) throws Exception {
        List<SyncEmployeeRequestEntity.SyncEmployeeInfoEntity> employeeList = employeeInfo.getEmployeeInfoList();
        List<SyncEmployeeCallBackRequestEntity.FailInfo> failInfos = new ArrayList<>();
        for (SyncEmployeeRequestEntity.SyncEmployeeInfoEntity employee:employeeList){
            Tuple2<ServiceErrorEnum, Long> resultTuple = null;
            if (MessageBizTypeEnum.SYNC_EMPLOYEE_ADD.getTags().equals(employee.getMessageBizTypeEnum().getTags())){
                resultTuple = this.addEmployee(employee);
            }else if (MessageBizTypeEnum.SYNC_EMPLOYEE_UPDATE.getTags().equals(employee.getMessageBizTypeEnum().getTags())){
                resultTuple = this.updateEmployee(employee);
            }else if (MessageBizTypeEnum.SYNC_EMPLOYEE_DELETE.getTags().equals(employee.getMessageBizTypeEnum().getTags())){
                resultTuple = this.deleteEmployee(employee);
            }

            if (resultTuple != null && !ServiceErrorEnum.Success.getErrorCode().equals(resultTuple.getFirst().getErrorCode())) {
                SyncEmployeeCallBackRequestEntity.FailInfo failInfo = new SyncEmployeeCallBackRequestEntity.FailInfo();
                failInfo.setLineNo(employee.getLineNo());
                failInfo.setFailReason(resultTuple.getFirst().getErrorMessage());
                failInfos.add(failInfo);
            }
        }

        MbImportExcelRecord mbImportExcelRecord = new MbImportExcelRecord();
        mbImportExcelRecord.setFileName(MbImportExcelRecordTypeEnum.EMP.getDesc());
        mbImportExcelRecord.setType(MbImportExcelRecordTypeEnum.EMP.getCode());
        mbImportExcelRecord.setUid("admin");
        mbImportExcelRecord.setTotalCount(employeeList.size());
        mbImportExcelRecord.setFailCount(failInfos.size());
        mbImportExcelRecord.setStatus(failInfos.size() > 0 ? MbImportExcelRecordStatusEnum.F.getDealCode() : MbImportExcelRecordStatusEnum.E.getDealCode());
        mbImportExcelRecord.setId(employeeInfo.getRecordId());
        mbImportExcelRecordService.updateRecord(mbImportExcelRecord);
        if (CollectionUtils.isNotEmpty(failInfos)){
            for (SyncEmployeeCallBackRequestEntity.FailInfo failInfo:failInfos){
                MbImportExcelRecordDetailLog detailLog = new MbImportExcelRecordDetailLog();
                detailLog.setRecordId(mbImportExcelRecord.getId());
                detailLog.setLineNo(failInfo.getLineNo());
                detailLog.setMessage(failInfo.getFailReason());
                detailLog.setStatus(MbImportExcelRecordStatusEnum.F.getDealCode());
                detailLog.setRecordId(employeeInfo.getRecordId());
                mbImportExcelRecordService.insertRecordDetailLog(detailLog);
            }
        }
        SyncEmployeeCallBackRequestEntity callBackRequestEntity = new SyncEmployeeCallBackRequestEntity();
        callBackRequestEntity.setRecordId(employeeInfo.getRecordId() == null ? "" : employeeInfo.getRecordId().toString());
        callBackRequestEntity.setActionTime(DateUtil.getNowDay());
        callBackRequestEntity.setTotalCount(employeeList.size());
        callBackRequestEntity.setFailCount(failInfos.size());
        callBackRequestEntity.setSuccessCount(employeeList.size() - failInfos.size());
        callBackRequestEntity.setAuthKey(Md5Util.md5Hex(callBackRequestEntity.getRecordId() +
                callBackRequestEntity.getTotalCount() + callBackRequestEntity.getFailCount()));
        callBackRequestEntity.setFailInfoList(failInfos);
        callBackResult(employeeInfo.getNotifyUrl(), callBackRequestEntity);
    }
    /**
     * 回调结果
     * @param callback
     */
    private void callBackResult(String notifyUrl, SyncEmployeeCallBackRequestEntity callback) {
        log.info("异步回调：{}，{}", notifyUrl, callback);
        if (StringUtils.isNotBlank(notifyUrl)) {
            try {
                HttpUtils.doPostJSON("","人员同步回调" ,notifyUrl, JsonUtils.toJsonString(callback));
            } catch (Exception e) {
                log.info("同步人员回调失败：{}", e);
            }
        }
    }
}
